import { InitRMB } from 'db://assets/battle/Const';
import { BuildingType, PlayerType, UnitType } from '../../../battle/Enums';
import { BuildingModel, UnitModel } from '../../../battle/Model';
import { LOG } from '../../../ConsoleLogCtrl';
/**
 * 玩家统计数据类
 * 管理玩家的基本信息和战斗统计数据
 */
export class PlayerStats {
    // 基本属性
    private id: string;
    private color: string;
    private startPosition: { x: number; y: number };
    private playerType: PlayerType;
    private rmb: number = InitRMB; // 玩家资源（RMB）

    // 单位和建筑集合
    private units: Map<number, UnitModel> = new Map();
    private buildings: Map<number, BuildingModel> = new Map();

    // 统计数据 - 按类型细分的单位统计
    private producedUnitsCount: Map<UnitType, number> = new Map([
        [UnitType.soldier, 0],    // 生产士兵数量
        [UnitType.collect, 0],    // 生产采集兵数量
        [UnitType.doctor, 0],     // 生产治疗兵数量
        [UnitType.archer, 0],     // 生产弓箭手数量
        [UnitType.worker, 0]      // 生产建造兵数量
    ]);
    private deadUnitsCount: Map<UnitType, number> = new Map([
        [UnitType.soldier, 0],    // 死亡士兵数量
        [UnitType.collect, 0],    // 死亡采集兵数量
        [UnitType.doctor, 0],     // 死亡治疗兵数量
        [UnitType.archer, 0],     // 死亡弓箭手数量
        [UnitType.worker, 0]      // 死亡建造兵数量
    ]);
    
    // 统计数据 - 按类型细分的建筑统计
    private builtBuildingsCount: Map<BuildingType, number> = new Map([
        [BuildingType.base, 0],   // 构建基地数量
        [BuildingType.tower, 0],  // 构建防御塔数量
        [BuildingType.wall, 0]    // 构建城墙数量
    ]);
    private destroyedBuildingsCount: Map<BuildingType, number> = new Map([
        [BuildingType.base, 0],   // 被摧毁基地数量
        [BuildingType.tower, 0],  // 被摧毁防御塔数量
        [BuildingType.wall, 0]    // 被摧毁城墙数量
    ]);
    private baseHealth: number = 0;                // 基地血量

    // 战斗统计数据
    private totalDamageDealt: number = 0;          // 造成的总伤害
    private totalDamageReceived: number = 0;       // 受到的总伤害
    private totalHealing: number = 0;              // 治疗总量
    private kills: number = 0;                     // 击杀数
    private deaths: number = 0;                    // 死亡数

    constructor(id: string, color: string, startPosition: { x: number; y: number }, playerType: PlayerType) {
        this.id = id;
        this.color = color;
        this.startPosition = { ...startPosition };
        this.playerType = playerType;
    }

    /**
     * 获取玩家ID
     */
    public getId(): string {
        return this.id;
    }

    /**
     * 获取玩家颜色
     */
    public getColor(): string {
        return this.color;
    }

    /**
     * 获取玩家起始位置
     */
    public getStartPosition(): { x: number; y: number } {
        return { ...this.startPosition };
    }

    /**
     * 获取玩家类型
     */
    public getPlayerType(): PlayerType {
        return this.playerType;
    }

    /**
     * 判断是否为左侧玩家（玩家1）
     */
    public isLeftSidePlayer(): boolean {
        return this.playerType === PlayerType.PLAYER1;
    }

    /**
     * 判断是否为右侧玩家（玩家2）
     */
    public isRightSidePlayer(): boolean {
        return this.playerType === PlayerType.PLAYER2;
    }

    /**
     * 添加单位
     * @param unit 单位模型
     */
    public addUnit(unit: UnitModel): void {
        this.units.set(unit.id, unit);
        const currentCount = this.producedUnitsCount.get(unit.type) || 0;
        this.producedUnitsCount.set(unit.type, currentCount + 1);
    }

    /**
     * 添加建筑
     * @param building 建筑模型
     */
    public addBuilding(building: BuildingModel): void {
        this.buildings.set(building.id, building);
        const currentCount = this.builtBuildingsCount.get(building.type) || 0;
        this.builtBuildingsCount.set(building.type, currentCount + 1);

        // 如果是基地，更新基地血量
        if (building.type === BuildingType.base) {
            this.baseHealth = building.hp;
        }
    }

    /**
     * 获取单位
     * @param unitId 单位ID
     */
    public getUnit(unitId: number): UnitModel | undefined {
        return this.units.get(unitId);
    }

    /**
     * 获取建筑
     * @param buildingId 建筑ID
     */
    public getBuilding(buildingId: number): BuildingModel | undefined {
        return this.buildings.get(buildingId);
    }

    /**
     * 获取所有单位
     */
    public getAllUnits(): UnitModel[] {
        return Array.from(this.units.values());
    }

    /**
     * 获取所有建筑
     */
    public getAllBuildings(): BuildingModel[] {
        return Array.from(this.buildings.values());
    }

    /**
     * 移除单位（标记为死亡）
     * @param unitId 单位ID
     */
    public removeUnit(unitId: number): boolean {
        const unit = this.units.get(unitId);
        if (unit) {
            const currentCount = this.deadUnitsCount.get(unit.type) || 0;
            this.deadUnitsCount.set(unit.type, currentCount + 1);
            return this.units.delete(unitId);
        }
        return false;
    }

    /**
     * 移除建筑（标记为被摧毁）
     * @param buildingId 建筑ID
     */
    public removeBuilding(buildingId: number): boolean {
        const building = this.buildings.get(buildingId);
        if (building) {
            const currentCount = this.destroyedBuildingsCount.get(building.type) || 0;
            this.destroyedBuildingsCount.set(building.type, currentCount + 1);
            return this.buildings.delete(buildingId);
        }
        return false;
    }

    /**
     * 更新单位状态
     * @param unitId 单位ID
     * @param hp 血量
     */
    public updateUnitHealth(unitId: number, hp: number): void {
        const unit = this.units.get(unitId);
        if (unit) {
            unit.hp = hp;
            if (hp <= 0) {
                this.removeUnit(unitId);
            }
        }
    }

    /**
     * 更新建筑状态
     * @param buildingId 建筑ID
     * @param hp 血量
     */
    public updateBuildingHealth(buildingId: number, hp: number): void {
        const building = this.buildings.get(buildingId);
        if (building) {
            building.hp = hp;

            // 如果是基地，更新基地血量
            if (building.type === BuildingType.base) {
                this.baseHealth = hp;
            }

            if (hp <= 0) {
                this.removeBuilding(buildingId);
            }
        }
    }

    /**
     * 获取生产单位总数量
     */
    public getProducedUnitsCount(): number {
        let total = 0;
        for (const count of this.producedUnitsCount.values()) {
            total += count;
        }
        return total;
    }

    /**
     * 获取指定类型的生产单位数量
     * @param unitType 单位类型
     */
    public getProducedUnitsCountByType(unitType: UnitType): number {
        return this.producedUnitsCount.get(unitType) || 0;
    }

    /**
     * 获取所有类型的生产单位数量统计
     */
    public getAllProducedUnitsCount(): Map<UnitType, number> {
        return new Map(this.producedUnitsCount);
    }

    /**
     * 获取死亡单位总数量
     */
    public getDeadUnitsCount(): number {
        let total = 0;
        for (const count of this.deadUnitsCount.values()) {
            total += count;
        }
        return total;
    }

    /**
     * 获取指定类型的死亡单位数量
     * @param unitType 单位类型
     */
    public getDeadUnitsCountByType(unitType: UnitType): number {
        return this.deadUnitsCount.get(unitType) || 0;
    }

    /**
     * 获取所有类型的死亡单位数量统计
     */
    public getAllDeadUnitsCount(): Map<UnitType, number> {
        return new Map(this.deadUnitsCount);
    }

    /**
     * 获取存活单位数量
     */
    public getAliveUnitsCount(): number {
        return this.units.size;
    }

    /**
     * 获取构建建筑总数量
     */
    public getBuiltBuildingsCount(): number {
        let total = 0;
        for (const count of this.builtBuildingsCount.values()) {
            total += count;
        }
        return total;
    }

    /**
     * 获取指定类型的构建建筑数量
     * @param buildingType 建筑类型
     */
    public getBuiltBuildingsCountByType(buildingType: BuildingType): number {
        return this.builtBuildingsCount.get(buildingType) || 0;
    }

    /**
     * 获取所有类型的构建建筑数量统计
     */
    public getAllBuiltBuildingsCount(): Map<BuildingType, number> {
        return new Map(this.builtBuildingsCount);
    }

    /**
     * 获取被摧毁建筑总数量
     */
    public getDestroyedBuildingsCount(): number {
        let total = 0;
        for (const count of this.destroyedBuildingsCount.values()) {
            total += count;
        }
        return total;
    }

    /**
     * 获取指定类型的被摧毁建筑数量
     * @param buildingType 建筑类型
     */
    public getDestroyedBuildingsCountByType(buildingType: BuildingType): number {
        return this.destroyedBuildingsCount.get(buildingType) || 0;
    }

    /**
     * 获取所有类型的被摧毁建筑数量统计
     */
    public getAllDestroyedBuildingsCount(): Map<BuildingType, number> {
        return new Map(this.destroyedBuildingsCount);
    }

    /**
     * 获取现存建筑数量
     */
    public getExistingBuildingsCount(): number {
        return this.buildings.size;
    }

    /**
     * 获取基地血量
     */
    public getBaseHealth(): number {
        return this.baseHealth;
    }

    /**
     * 重置统计数据
     */
    public resetStats(): void {
        this.units.clear();
        this.buildings.clear();
        
        // 重置单位统计
        this.producedUnitsCount.clear();
        this.producedUnitsCount.set(UnitType.soldier, 0);
        this.producedUnitsCount.set(UnitType.collect, 0);
        this.producedUnitsCount.set(UnitType.doctor, 0);
        this.producedUnitsCount.set(UnitType.archer, 0);
        this.producedUnitsCount.set(UnitType.worker, 0);
        
        this.deadUnitsCount.clear();
        this.deadUnitsCount.set(UnitType.soldier, 0);
        this.deadUnitsCount.set(UnitType.collect, 0);
        this.deadUnitsCount.set(UnitType.doctor, 0);
        this.deadUnitsCount.set(UnitType.archer, 0);
        this.deadUnitsCount.set(UnitType.worker, 0);
        
        // 重置建筑统计
        this.builtBuildingsCount.clear();
        this.builtBuildingsCount.set(BuildingType.base, 0);
        this.builtBuildingsCount.set(BuildingType.tower, 0);
        this.builtBuildingsCount.set(BuildingType.wall, 0);
        
        this.destroyedBuildingsCount.clear();
        this.destroyedBuildingsCount.set(BuildingType.base, 0);
        this.destroyedBuildingsCount.set(BuildingType.tower, 0);
        this.destroyedBuildingsCount.set(BuildingType.wall, 0);
        
        this.baseHealth = 0;
        this.rmb = 0; // 重置资源
        
        // 重置战斗统计数据
        this.totalDamageDealt = 0;
        this.totalDamageReceived = 0;
        this.totalHealing = 0;
        this.kills = 0;
        this.deaths = 0;
    }

    /**
     * 获取玩家资源（RMB）
     */
    public getRmb(): number {
        return this.rmb;
    }

    /**
     * 增加玩家资源（RMB）
     * @param value 增加的资源值
     */
    public addRmb(value: number): void {
        this.rmb += value;
    }

    /**
     * 获取造成的总伤害
     */
    public getTotalDamageDealt(): number {
        return this.totalDamageDealt;
    }

    /**
     * 添加造成的伤害
     * @param damage 伤害值
     */
    public addDamageDealt(damage: number): void {
        this.totalDamageDealt += damage;
    }

    /**
     * 获取受到的总伤害
     */
    public getTotalDamageReceived(): number {
        return this.totalDamageReceived;
    }

    /**
     * 添加受到的伤害
     * @param damage 伤害值
     */
    public addDamageReceived(damage: number): void {
        this.totalDamageReceived += damage;
    }

    /**
     * 获取治疗总量
     */
    public getTotalHealing(): number {
        return this.totalHealing;
    }

    /**
     * 添加治疗量
     * @param healing 治疗值
     */
    public addHealing(healing: number): void {
        this.totalHealing += healing;
    }

    /**
     * 获取击杀数
     */
    public getKills(): number {
        return this.kills;
    }

    /**
     * 添加击杀数
     */
    public addKill(): void {
        this.kills++;
    }

    /**
     * 获取死亡数
     */
    public getDeaths(): number {
        return this.deaths;
    }

    /**
     * 添加死亡次数
     */
    public addDeath(): void {
        this.deaths++;
    }
}